home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C25 / DynaTrash.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.5 KB  |  59 lines

  1. //: C25:DynaTrash.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. //{L} TrashPrototypeInit
  7. //{L} fillBin Trash TrashStatics
  8. // Using a map of vectors and RTTI
  9. // to automatically sort Trash into
  10. // vectors. This solution, despite the
  11. // use of RTTI, is extensible.
  12. #include "Trash.h"
  13. #include "fillBin.h"
  14. #include "sumValue.h"
  15. #include "../purge.h"
  16. #include <iostream>
  17. #include <fstream>
  18. #include <vector>
  19. #include <map>
  20. #include <typeinfo>
  21. using namespace std;
  22. ofstream out("DynaTrash.out");
  23.  
  24. // Must adapt from type_info in Standard C++,
  25. // since type_info is too restrictive:
  26. template<class T> // T should be a base class
  27. class TypeInfo {
  28.   string id;
  29. public:
  30.   TypeInfo(T* t) : id(typeid(*t).name()) {}
  31.   const string& name() { return id; }
  32.   friend bool operator<(const TypeInfo& lv,
  33.     const TypeInfo& rv){
  34.     return lv.id < rv.id;
  35.   }
  36. };
  37.  
  38. class TypeMap : 
  39.   public map<TypeInfo<Trash>, vector<Trash*> >,
  40.   public Fillable {
  41. public:
  42.   // Satisfies the Fillable interface:
  43.   void addTrash(Trash* t) {
  44.     (*this)[TypeInfo<Trash>(t)].push_back(t);
  45.   }
  46.   ~TypeMap() {
  47.     for(iterator it = begin(); it != end(); it++)
  48.       purge((*it).second);
  49.   }
  50. };
  51.  
  52. int main() {
  53.   TypeMap bin;
  54.   fillBin("Trash.dat", bin); // Sorting happens
  55.   TypeMap::iterator it;
  56.   for(it = bin.begin(); it != bin.end(); it++)
  57.     sumValue((*it).second);
  58. } ///:~
  59.